package com.iseastar.guojiang.app;

import android.bluetooth.BluetoothDevice;

import com.iseastar.guojiang.model.BluetoothDeviceBean;
import com.iseastar.guojiang.model.CabinetBean;
import com.iseastar.guojiang.model.IDCardInfoBean;
import com.iseastar.guojiang.model.OrderBean;
import com.iseastar.guojiang.model.UserBean;
import com.shouhuobao.station.wxapi.Constants;
import com.shouhuobao.take.model.StationOpenInfoBean;

import droid.frame.App;
import droid.frame.utils.lang.ObjectSerialize;
import droid.frame.utils.lang.Str;

/**
 * 裹将中的数据缓存
 *
 * @author coffee<br>
 * 2016-11-29下午2:15:41
 */
public class AppCache extends droid.frame.utils.lang.Cache {
    /**
     * 账号信息
     */
    private static UserBean user;
    /**
     * 当前正在操作的 [柜子信息|驿站信息]
     */
    private static CabinetBean.StationCabubetBean cabinetOrStation;

    /**
     * 当前的订单Id
     */
    private static String orderId;

    @Deprecated
    private static OrderBean order;
    private static String parcelId;

    private static BluetoothDeviceBean bluetoothDeviceBean;

    /**
     * 驿站申请开通对象
     */
    private static StationOpenInfoBean stationOpenInfoBean;
    /**
     * 火箭侠身份识别对象
     */
    private static IDCardInfoBean idCardInfoBean;
    /**
     * 记录是否展示活动提示
     */
    private static Integer activeTips;

    public static UserBean getUser() {
        if (user == null) {
            //            user = ObjectSerialize.read(UserBean.class, null);
            user = App.getCurrAccount().getObject(Constants.STATION_USER_INFO, UserBean.class);
        }
        if (user == null) {
            user = new UserBean();
        }
        return user;
    }

    public static void setUser(UserBean user) {
        AppCache.user = user;
        if (user == null) {
            //            ObjectSerialize.remove(UserBean.class, null);
            App.getCurrAccount().setObject(Constants.STATION_USER_INFO, null);
        } else {
            //            ObjectSerialize.write(user, null);
            App.getCurrAccount().setObject(Constants.STATION_USER_INFO, user);
        }
    }

    /**
     * @return
     */
    public static CabinetBean.StationCabubetBean getCabinetOrStation() {
        return cabinetOrStation;
    }

    public static void setCabinetOrStation(CabinetBean.StationCabubetBean cabinetOrStation) {
        AppCache.cabinetOrStation = cabinetOrStation;
    }

    public static String getOrderId() {
        if (Str.isEmpty(orderId) || Integer.parseInt(orderId) == 0) {
            OrderBean order = getOrder();
            if (order != null) {
                orderId = order.getId();
            }
        }
        return orderId;
    }

    public static void setOrderId(String orderId) {
        AppCache.orderId = orderId;
    }

    public static OrderBean getOrder() {
        if (order == null) {
            order = ObjectSerialize.read(OrderBean.class, null);
        }
        return order;
    }

    public static void setOrder(OrderBean order) {
        AppCache.order = order;
        if (order == null) {
            AppCache.remove(OrderBean.class);
            AppCache.setOrderId(0 + "");
            AppCache.setParcelId(0 + "");
        } else {
            setParcelId(order.getParcelId());
            setOrderId(order.getId());
            asyncWrite(order, null);
        }
    }

    public static String getParcelId() {
        return parcelId;
    }

    public static void setParcelId(String parcelId) {
        AppCache.parcelId = parcelId;
    }

    public static BluetoothDeviceBean getBluetoothDeviceBean() {
        if (bluetoothDeviceBean == null) {
            bluetoothDeviceBean = ObjectSerialize.read(BluetoothDeviceBean.class, null);
        }
        return bluetoothDeviceBean;
    }

    public static void setBluetoothDeviceBean(BluetoothDeviceBean bluetoothDeviceBean) {
        AppCache.bluetoothDeviceBean = bluetoothDeviceBean;
        if (bluetoothDeviceBean == null) {
            AppCache.remove(BluetoothDevice.class);
        } else {
            asyncWrite(bluetoothDeviceBean, null);
        }
    }

    public static StationOpenInfoBean getStationOpenInfoBean() {
        if (stationOpenInfoBean == null) {
            stationOpenInfoBean = ObjectSerialize.read(StationOpenInfoBean.class, null);
        }
        return stationOpenInfoBean;
    }

    public static void setStationOpenInfoBean(StationOpenInfoBean stationOpenInfoBean) {
        AppCache.stationOpenInfoBean = stationOpenInfoBean;
        if (stationOpenInfoBean == null) {
            AppCache.remove(StationOpenInfoBean.class);
        } else {
            asyncWrite(stationOpenInfoBean, null);
        }
    }

    public static IDCardInfoBean getIdCardInfoBean() {
        if (idCardInfoBean == null) {
            idCardInfoBean = ObjectSerialize.read(IDCardInfoBean.class, null);
        }
        return idCardInfoBean;
    }

    public static void setIdCardInfoBean(IDCardInfoBean idCardInfoBean) {
        AppCache.idCardInfoBean = idCardInfoBean;
        if (idCardInfoBean == null) {
            AppCache.remove(IDCardInfoBean.class);
        } else {
            asyncWrite(idCardInfoBean, null);
        }
    }
}